package com.jic.member.controller.manage;

import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.RestResult;
import com.jic.member.constant.consist.ResponseCode;
import com.jic.member.entity.TGroupLabelRelation;
import com.jic.member.request.TGroupLabelRelationRequest;
import com.jic.member.response.TGroupLabelRelationResponse;
import com.jic.member.service.TGroupLabelRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 建投数据科技(山东)有限公司
 * @date: 2020-06-04 15:47:06
 */
@RestController
@Slf4j
@RequestMapping("/tGroupLabelRelation")
@Api(tags = " [用户组标签关联表]控制类-PC")
public class TGroupLabelRelationController {
    @Autowired
    private TGroupLabelRelationService service;

    /**
     * 新增记录
     *
     * @param request 新增[用户组标签关联表]请求参数
     * @return TGroupLabelRelationResponse 新增[用户组标签关联表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "新增[用户组标签关联表]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody TGroupLabelRelationRequest request) throws Throwable {
        TGroupLabelRelation tGroupLabelRelation = new TGroupLabelRelation();
        BeanUtils.copyProperties(request, tGroupLabelRelation);
        long flag = service.save(tGroupLabelRelation);
        return RestResult.success(flag);
    }

    /**
     * 新增或修改记录
     *
     * @param request 新增或修改[用户组标签关联表]请求参数
     * @return TGroupLabelRelationResponse 新增或修改[用户组标签关联表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "保存或更新[用户组标签关联表]")
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public RestResult<Long> saveOrUpdate(@Validated @RequestBody TGroupLabelRelationRequest request) throws Throwable {
        TGroupLabelRelation tGroupLabelRelation = new TGroupLabelRelation();
        BeanUtils.copyProperties(request, tGroupLabelRelation);
        long flag = service.saveOrUpdate(tGroupLabelRelation);
        return RestResult.success(flag);
    }

    /**
     * 删除记录
     *
     * @param request 删除[用户组标签关联表]请求参数
     * @return TGroupLabelRelationResponse 删除[用户组标签关联表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "删除[用户组标签关联表]记录")
    @PostMapping("/delete")
    @ResponseBody
    public RestResult<Long> delete(@RequestBody TGroupLabelRelationRequest request) throws Throwable {
        TGroupLabelRelation tGroupLabelRelation = new TGroupLabelRelation();
        BeanUtils.copyProperties(request, tGroupLabelRelation);
        long flag = service.deleteByPrimaryKeyLogically(tGroupLabelRelation);
        return RestResult.success(flag);
    }

    /**
     * 修改记录
     *
     * @param request 修改[用户组标签关联表]请求参数
     * @return TGroupLabelRelationResponse 修改[用户组标签关联表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "修改[用户组标签关联表]记录")
    @PostMapping("/update")
    @ResponseBody
    public RestResult<Long> update(@RequestBody TGroupLabelRelationRequest request) throws Throwable {
        TGroupLabelRelation tGroupLabelRelation = new TGroupLabelRelation();
        BeanUtils.copyProperties(request, tGroupLabelRelation);
        long flag = service.updateByPrimaryKeySelective(tGroupLabelRelation);
        return RestResult.success(flag);
    }

    /**
     * 单表查询详情信息
     *
     * @param request 查询[用户组标签关联表]详情请求参数
     * @return TGroupLabelRelationResponse 查询[用户组标签关联表]详情响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @PostMapping("/getByPrimaryKey")
    @ApiOperation(value = "查询[用户组标签关联表]详情")
    @ResponseBody
    public RestResult<TGroupLabelRelationResponse> getByPrimaryKey(@Validated @RequestBody TGroupLabelRelationRequest request) {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        if (request != null && request.getId() != null) {

            TGroupLabelRelation tGroupLabelRelation = new TGroupLabelRelation();
            BeanUtils.copyProperties(request, tGroupLabelRelation);
            TGroupLabelRelation result = service.getByPrimaryKey(tGroupLabelRelation);
            log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
            if (result == null) {
                return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
            }
            TGroupLabelRelationResponse bo = new TGroupLabelRelationResponse();
            BeanUtils.copyProperties(result, bo);
            return RestResult.success(bo);
        }
        log.info("查询详情请求结束");
        return RestResult.success(new TGroupLabelRelationResponse());
    }


    /**
     * T_GROUP_LABEL_RELATION 查询列表（不分页）
     *
     * @param request 前端请求参数
     */
    @PostMapping("/listAll")
    @ApiOperation(value = "不分页查询 [用户组标签关联表] ")
    @ResponseBody
    public RestResult<List<TGroupLabelRelationResponse>> listTGroupLabelRelationAllRecord(@RequestBody TGroupLabelRelationRequest request) throws Throwable {
        TGroupLabelRelation param = new TGroupLabelRelation();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        List<TGroupLabelRelation> list = service.listAllRecord(param);
        List<TGroupLabelRelationResponse> responseList = list.stream().map(temp -> {
            TGroupLabelRelationResponse bo = new TGroupLabelRelationResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }

    /**
     * T_GROUP_LABEL_RELATION 查询标签数
     *
     * @param request 前端请求参数
     */
    @PostMapping("/getUserGroupLabelCount")
    @ApiOperation(value = "得到标签数[用户组标签关联表] ")
    @ResponseBody
    public RestResult<Integer> getUserGroupLabelCount(@RequestBody TGroupLabelRelationRequest request) throws Throwable {
        TGroupLabelRelation relation = new TGroupLabelRelation();
        BeanUtils.copyProperties(request, relation);
        return service.getUserGroupLabelCount(relation);
    }

}

